package com.shixu.framework.plugins.qyweixin.service;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shixu.express.qyweixin.weixin.WeChat;
import com.shixu.framework.core.AppContext;
import com.shixu.framework.plugins.qyweixin.util.HttpKit;

/**
 * 微信Oauth和支付工具类
 *
 */
public class Oauth {
	private static Logger log = LoggerFactory.getLogger(Oauth.class);  
	
    private static final String CODE_URI = "http://open.weixin.qq.com/connect/oauth2/authorize";
    private static final String GET_USERINFO_BY_CODE = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
    
    private String appid;
    private String secret;

	public Oauth() {
        super();
        this.appid = AppContext.getInstance().getString("corpid");
        this.secret = AppContext.getInstance().getString("appsecret");
    }

    public Oauth(String appid, String secret) {
        super();
        this.appid = appid;
        this.secret = secret;
    }

    /**
     * 请求code
     * @return
     * @throws UnsupportedEncodingException 
     */
    public String getCode() throws UnsupportedEncodingException {
        
        return getCode(AppContext.getInstance().getString("qy_redirect_uri"));
    }

    /**
     * 请求code
     * @return
     * @throws UnsupportedEncodingException 
     */
    public String getCode(String url) throws UnsupportedEncodingException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", getAppid());
        params.put("redirect_uri", url);
        params.put("response_type", "code");
        params.put("scope", "snsapi_base"); // snsapi_base（不弹出授权页面，只能拿到用户openid）snsapi_userinfo
        // （弹出授权页面，这个可以通过 openid 拿到昵称、性别、所在地）
        params.put("state", "wx#wechat_redirect");
        String para = Pay.createSign(params, true);
        
        return CODE_URI + "?" + para;
    }
    
    /**
     * 根据code获取成员信息
     * @param code
     * @return string 	UserId  员工UserID  
						DeviceId  手机设备号(由微信在安装时随机生成)  
     * @throws Exception 
     */
    public String getUserInfo(String code) throws Exception{
//    	access_token  是  调用接口凭证  
//    	code  是  通过员工授权获取到的code，每次员工授权带上的code将不一样，code只能使用一次，5分钟未被使用自动过期  
//    	agentid  是  跳转链接时所在的企业应用ID 
    	String accessToken = WeChat.getAccessToken().getToken();
    	String agentid = AppContext.getInstance().getString("agentid");
    	Map<String, String> params = new HashMap<String, String>();
        params.put("access_token", accessToken);
        params.put("code", code);
        params.put("agentid", "1");
    	
        String jsonStr = HttpKit.get(GET_USERINFO_BY_CODE,params);
    	return jsonStr;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }
}
